package com.yanqu.road.server.channelserver.timer.tonbing;

import com.yanqu.road.server.channelserver.manager.activity.tonbing.ChannelTonBingMgr;
import com.yanqu.road.server.channelserver.manager.activity.tonbing.activity.ChannelTonBingActivity;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

/**
 * 放统兵演武一些不需要及时的定时任务
 */
public class ChannelTonBingTimerTask extends GeneralTask {

    private long lastCalcBattacks = 0L;

    private long lastPrintTime = 0L;

    @Override
    public void doRun() {

        long now = System.currentTimeMillis();

        // 【计算全部兵营】
        if (now - lastCalcBattacks > DateHelper.MINUTE_MILLIONS) {
            lastCalcBattacks = now;
            for (ChannelTonBingActivity activity : ChannelTonBingMgr.getActivityMap().values()) {
                if (activity.getConfig().inEnterActivityTime()) {
                    try {
                        activity.calcAllBarracks();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        // 【打印】
        if (now - lastPrintTime > DateHelper.MINUTE_MILLIONS) {
            lastPrintTime = now;
            for (ChannelTonBingActivity activity : ChannelTonBingMgr.getActivityMap().values()) {
                try {
                    activity.printTask();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        for (ChannelTonBingActivity activity : ChannelTonBingMgr.getActivityMap().values()) {
            try {
                activity.makeNpcEvent();
                activity.syncRedPacketBoxValue();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        // 【自动发城池采集奖励】
        for (ChannelTonBingActivity activity : ChannelTonBingMgr.getActivityMap().values()) {
            try {
                activity.mailSendCityCollect();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}
